<?PHP
/*

OpenTrack API PHP Framework

This software is written to expedite clean and correct API calls to the DealerTrack Webservices
collectively known as "OpenTrack".  This framework is a simple introduction to how the API
works.  There are areas of incompleteness and oppourtunities for improvement.  If there are
features you need assistance with please contact jarrett@crownmotors.pro

Open Source Initiative OSI - The MIT License:Licensing

The MIT License

We have no copyright on this code. Infact this code is not worthy
of a copyright. However if it helps you in anyway
please send thanks to Jarrett Kuljis @ jarrett@crownmotors.pro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/
/*#############################
    * VehicleSearch Request
    * VehicleLookup Request
    * VehicleInventory Request
    * VehicleInventoryCost Request
    * VehicleAdd Request
    * VehicleUpdate Request
    * VehicleSold Request
    * Vehicle Table Requests 
*/#############################

class Vehicle extends ArkonaDMS_Interop
{

	public function VehicleUpdate($ua) {
	
		if(!is_array($ua)) {
			parent::eLog("VehicleUpdate() method failed, array expected as primary argument.");
			exit();
		}
		
		$sp = '<VehicleUpdate>';
		$sp .= parent::addDealerCode();
		$sp .= '<Vehicle>';
		
			// Get valid update fields and execute.
			foreach($ua as $k => $f) {
				$sp .= '<'.$k.'>'.$f.'</'.$k.'>';
			}
			
		$sp .= '</Vehicle>';
		$sp .= '</VehicleUpdate>';
		
		parent::aLog("VehicleUpdate() executed for Stock# ".$ua['StockNumber']." ");
		return parent::execRequest($sp);
		
	}
	
	public function VehicleSearch($ua) {
	
		if(!is_array($ua)) {
			parent::eLog("VehicleSearch() method failed, array expected as primary argument.");
			exit();
		}
		
		$sp = '<VehicleSearch>';
		$sp .= parent::addDealerCode();
		$sp .= '<SearchParms>';
		
			// Get valid update fields and execute.
			foreach($ua as $k => $f) {
				$sp .= '<'.$k.'>'.$f.'</'.$k.'>';	//    VIN=>  LastSixOfVIN=>  StockNumber=>  Make=>  Model=>  ModelYear=>  Type=>  StockDateStart=>  StockDateEnd=> 
			}
			
		$sp .= '</SearchParms>';
		$sp .= '</VehicleSearch>';
		
		parent::aLog("VehicleSearch() executed for ".var_dump($ua));
		return parent::execRequest($sp);
		
	}	
	
	public function VehicleLookup($ua) {
	
		if(!is_array($ua)) {
			parent::eLog("VehicleLookup() method failed, array expected as primary argument.");
			exit();
		}
		
		$sp = '<VehicleLookup>';
		$sp .= parent::addDealerCode();
		
			// Get valid update fields and execute.
			foreach($ua as $k => $f) {
				$sp .= '<'.$k.'>'.$f.'</'.$k.'>';  // VIN=>  StockNumber=>
			}
			
		$sp .= '</VehicleLookup>';
		
		parent::aLog("VehicleLookup() executed for ".var_dump($ua));
		return parent::execRequest($sp);
		
	}		
	
	public function VehicleInventory($ua) {
	
		if(!is_array($ua)) {
			parent::eLog("VehicleInventory() method failed, array expected as primary argument.");
			exit();
		}
		
		$sp = '<VehicleInventory>';
		$sp .= parent::addDealerCode();
		$sp .= '<LookupParms>';
		
			// Get valid update fields and execute.
			foreach($ua as $k => $f) {
				$sp .= '<'.$k.'>'.$f.'</'.$k.'>';  //     Type=>  FromDateInInventory=>   ToDateInInventory =>  ( date format 20090501)
			}
		
		$sp .= '</LookupParms>';	
		$sp .= '</VehicleInventory>';
		
		parent::aLog("VehicleInventory() executed for ".var_dump($ua));
		return parent::execRequest($sp);
		
	}		

	public function VehicleInventoryCost() {
	
		//if(!is_array($ua)) {
		//	parent::eLog("VehicleInventoryCost() method failed, array expected as primary argument.");
		//	exit();
		//}
		
		$sp = '<VehicleInventoryCost>';
		$sp .= parent::addDealerCode();
		$sp .= '</VehicleInventoryCost>';
		
		parent::aLog("VehicleInventoryCost() executed ");
		return parent::execRequest($sp);
		
	}		

	public function VehicleAdd($af) {
		
		/* REQUIRED FIELDS
		
		VIN  	 						Y  	A17
		StockNumber 					Y 	A9
		DocumentNumber 					N 	A9 	Populated (usually with stock number) when GLentries are posted
		Status 							Y 	A1 	I=Inventory, C=Customer 
		TypeNU  	 					Y  	A1 	N=New, U=Used, D=Demo, R=Rental
		BusinessOfficeFranchiseCode  	Y  	A3  Use SaleGroupLookup for valid codes (VehicleSaleGroupTableRequest?)  
		ModelYear  	 					Y  	N4,0  	 CCYY 
		Make  	 						Y  	A25
		Model  	 						Y  	A25
		
		-- See DealerTrack Optional Fields for Required Options Fields 
		OptionNumber  	 				Y  	N3,0 Use VehicleOptionalFieldLookup request for valid option numbers; 
												 required if optional fields are populated; 
												 records with invalid option numbers will be discarded									  
		*/
		
		if(!is_array($af)) {
			parent::eLog("VehicleAdd method requires array as primary argument");
			exit();
		}
		
		if($af['VIN'] == '') { parent::eLog("VehicleAdd VIN required field not present"); return false; }
		if(strlen($af['VIN']) > 17) { parent::eLog("VehicleAdd VIN Length greater than 17 digits"); return false; }
		if($af['StockNumber'] == '' or (strlen($af['StockNumber']) > 9)) { parent::eLog("VehicleAdd StockNumber blank or greater than 9 chars"); return false; }
		if($af['Status'] != 'I' and $af['Status'] != 'C') { parent::eLog("VehicleAdd Status value invalid, acceptable values are I,C"); return false; }
		if($af['TypeNU'] != 'N' and $af['TypeNU'] != 'U' and $af['TypeNU'] != 'D' and $af['TypeNU'] != 'R') {
			parent::eLog("VehicleAdd TypeNU value invalid, acceptable values are N,U,D,R"); return false;
		}
		if($af['BusinessOfficeFranchiseCode'] == '' or (strlen($af['BusinessOfficeFranchiseCode']) > 3)) {
			parent::eLog("VehicleAdd BusinessOfficeFranchiseCode value invalid or length greater than 3"); return false;
		}
		if($af['ModelYear'] == '' or (strlen($af['ModelYear']) > 4)) { parent::eLog("VehicleAdd ModelYear greater than 4 or blank (CCYY)"); return false; }
		if($af['Make'] == '') { parent::eLog("VehicleAdd required field Make is empty"); return false; }
		if($af['Model'] == '') { parent::eLog("VehicleAdd required field Model is empty"); return false; }
		
		// Perform Lookup to ensure Vehicle is not in DataSet
		$vlkup	=	Vehicle::VehicleLookup(array( "VIN"=>$af['VIN'] ));
		
		if(strval($sdat->{'content'}->{'CustomerSearchResponse'}->{'Error'}->{'Code'}) != 'VL02') { 
			parent::eLog("VehicleAdd method failed, VIN# ".$af['VIN']." exists in dataset, unable to add...");
			return false;
		}
				
		$sp  = '<VehicleAdd>';
		$sp .=  parent::adddealerCode();
		$sp .= '	<Vehicle>';
			
			// Create fields for custom insert / add
			foreach($af as $k => $f) {
				$sp .= '<'.$k.'>'.$f.'</'.$k.'>';
			}		
					
		$sp .= '	</Vehicle>';
		$sp .= '</VehicleAdd>';
	
		parent::aLog("Vehicle Add Method");
		return parent::execRequest($sp);		
	}
	
	public function VehicleSold($sa) {
		
		if(!is_array($sa)) {
			parent::eLog("VehicleSold() requires array as argument");
			exit();
		}
		
		if($sa['VIN'] =='') { parent::eLog("VehicleSold() not given VIN#"); exit(); }
		if($sa['CustomerNumber'] =='') { parent::eLog("VehicleSold() not given CustomerNumber"); exit(); }
		if($sa['SalespersonID'] =='') { parent::eLog("VehicleSold() not given SalespersonID"); exit(); }
		if($sa['DateSold'] =='') { parent::eLog("VehicleSold() not given VIN#"); exit(); }
		
		$sp  = '<VehicleSold>';
		$sp .= parent::addDealerCode();
		$sp .= '<VIN>'.$sa['VIN'].'</VIN>';
		$sp .= '<CustomerNumber>'.$sa['CustomerNumber'].'</CustomerNumber>';
		$sp .= '<SalespersonID>'.$sa['SalespersonID'].'</SalespersonID>';
		$sp .= '<DateSold>'.$sa['DateSold'].'</DateSold>';
		// Check for Trade Ins
		if($sa['TradeId_1'] != '') {
			$sp .= '<TradeIns>';
			$sp .= '<VIN>'.$sa['TradeId_1'].'</VIN>';
			if($sa['TradeId_2'] != '') {
				$sp .= '<VIN>'.$sa['TradeId_2'].'</VIN>';
				if($sa['TradeId_3'] != '') {
					$sp .= '<VIN>'.$sa['TradeId_3'].'</VIN>';
				}
			}
			$sp .= '</TradeIns>';
		}
		$sp .= '</VehicleSold>';
		
		parent::aLog("VehicleSold() requested and executed. VIN# ".$sa['VIN']);
		parent::execRequest($sp);
	}

	############### TABLE REQUESTS #####################################
	public function VehicleOptionsTable() {
	
		$sp  = '<VehicleOptionsTableRequest>';
		$sp .= parent::addDealerCode();
		$sp .= '</VehicleOptionsTableRequest>';
		
		parent::aLog("VehicleOptions Table request");
		return parent::execRequest($sp);	
	}
	
	public function VehicleOptionalFieldsTable() {
	
		$sp  = '<VehicleOptionalFieldsTableRequest>';
		$sp .= parent::addDealerCode();
		$sp .= '</VehicleOptionalFieldsTableRequest>';
		
		parent::aLog("VehicleOptionalFields Table request");
		return parent::execRequest($sp);	
	}
	
	public function VehicleSaleAccountsTable() {
	
		$sp  = '<VehicleSaleAccountsTableRequest>';
		$sp .= parent::addDealerCode();
		$sp .= '</VehicleSaleAccountsTableRequest>';
		
		parent::aLog("VehicleSaleAccounts Table request");
		return parent::execRequest($sp);	
	}	
	
	public function VehicleSaleGroupsTable() {
	
		$sp  = '<VehicleSaleGroupsTableRequest>';
		$sp .= parent::addDealerCode();
		$sp .= '</VehicleSaleGroupsTableRequest>';
		
		parent::aLog("VehicleSaleGroupsTable Table request");
		return parent::execRequest($sp);	
	}		
}
?>